package cn.it.ygh.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.it.ygh.pojo.Apply;
import cn.it.ygh.pojo.House;
import cn.it.ygh.pojo.HouseInfo;
import cn.it.ygh.pojo.Page;
import cn.it.ygh.pojo.User;
import cn.it.ygh.pojo.Renter;
import cn.it.ygh.service.HouseDealService;
import cn.it.ygh.utils.FastJsonUtil;
@Controller(value="houseDealController")
@RequestMapping("/houseDeal")
public class HouseDealController {
	@Resource
	private  HouseDealService houseDealService;
	
	/*@RequestMapping("/getHouseInfo.do")
	public ModelAndView getHouseInfo(int minScale,int maxScale){
		ModelAndView modelAndView = new ModelAndView();
		
		List<HouseInfo> houseInfoList = houseDealService.getHouseInfo(minScale,maxScale);
		modelAndView.addObject("houseInfoList", houseInfoList);
		modelAndView.setViewName("../houseDeal/jsp/houseInfoSearch");
		return modelAndView;
	}*/
	
	@RequestMapping("/buy.do")
	public String buy(@RequestParam(value = "currentPage", defaultValue = "1", required = false) int currentPage, Model model) {
		Page<HouseInfo> pHI = houseDealService.findByPage(currentPage,0,1000000000,0,1000,0,"");
		model.addAttribute("pageHouseInfo",pHI==null?new Page<HouseInfo>():pHI);// 回显分页数据
		return "../houseDeal/jsp/BuyHouse";
	}
	@RequestMapping("/totalRent.do")
	public String totalRent(@RequestParam(value = "currentPage", defaultValue = "1", required = false) int currentPage, Model model) {
		Page<HouseInfo> pHI = houseDealService.findByPage1(currentPage,0,100000,0,"");
		System.out.print(pHI.getLists().size());
		model.addAttribute("pageHouseInfo",pHI==null?new Page<HouseInfo>():pHI);// 回显分页数据
		
		return "../houseDeal/jsp/TotalRentHouse";
	}
	
	@RequestMapping("/divRent.do")
	public String divRent(@RequestParam(value = "currentPage", defaultValue = "1", required = false) int currentPage, Model model) {
		Page<HouseInfo> pHI = houseDealService.findByPage2(currentPage,0,100000,0,"");
		model.addAttribute("pageHouseInfo",pHI==null?new Page<HouseInfo>():pHI);// 回显分页数据
		return "../houseDeal/jsp/DivRentHouse";
	}
	
	@RequestMapping("/renter.do")
	public String renter(@RequestParam(value = "currentPage", defaultValue = "1", required = false) int currentPage, 
			@RequestParam(value="houseId")int houseId,Model model) {
		Page<Renter> pHI = houseDealService.findByPage3(currentPage,houseId);
		model.addAttribute("pageRenter",pHI==null?new Page<HouseInfo>():pHI);// 回显分页数据
		return "../houseDeal/jsp/Renter";
	}
	
	@RequestMapping("/buyHouseDetail.do")
	public String buyHouseDetail(int id, Model model) {
		HouseInfo pHI = houseDealService.getIdInfo(id);
		model.addAttribute("iHI",pHI);
		return "../houseDeal/jsp/BuyHouseDetail";
	}
	@RequestMapping("/totalRentHouseDetail.do")
	public String totalRentHouseDetail(int id, Model model) {
		HouseInfo pHI = houseDealService.getIdInfo(id);
		model.addAttribute("iHI",pHI);
		return "../houseDeal/jsp/TotalRentHouseDetail";
	}
	@RequestMapping("/divRentHouseDetail.do")
	public String divRentHouseDetail(int id, Model model) {
		HouseInfo pHI = houseDealService.getIdInfo(id);
		model.addAttribute("iHI",pHI);
		return "../houseDeal/jsp/DivRentHouseDetail";
	}
	
	@RequestMapping(value="/submitApply.do",method=RequestMethod.POST)
	@ResponseBody
	public String submitApply(@RequestParam(value="houseId")int houseId,@RequestParam(value="houseAgentAccount")String houseAgentAccount,
			@RequestParam(value="houseAddress")String houseAddress,@RequestParam(value="houseProvince")String houseProvince,
			@RequestParam(value="houseCity")String houseCity,HttpSession session) {	
		String userAccount = ((User)session.getAttribute("u")).getUserAccount();
		Date applyTime = new Date();
		houseDealService.saveDataInDB(houseId,userAccount,houseAgentAccount,applyTime,houseAddress,houseProvince,houseCity);
		return "success";
	}
	
	@RequestMapping(value="/submitRenterApply.do",method=RequestMethod.POST)
	@ResponseBody
	public String submitRenterApply(@RequestParam(value="houseId")int houseId,@RequestParam(value="houseAddress")String houseAddress,
			@RequestParam(value="houseProvince")String houseProvince,@RequestParam(value="houseCity")String houseCity,HttpSession session) {	
		User u = (User)session.getAttribute("u");
		System.out.println(houseId+houseAddress+houseCity);
		houseDealService.saveDataInDB1(houseId,houseAddress,houseProvince,houseCity,u);
		return "../houseDeal/renter.do";
	}
	
	@RequestMapping("house-buy-ajax.do")
	@ResponseBody
	public Page<HouseInfo> houseBuyAjax(@RequestParam("condition") String condition, @RequestParam(value = "currentPage", defaultValue = "1", required = false) int currentPage) {
		JSONObject o = JSON.parseObject(condition); 
		JSONObject price = o.getJSONObject("price");
		JSONObject area = o.getJSONObject("area");
		Integer layout = o.getInteger("layout");
		String keyword = o.getString("keyword");
		
		int minPrice = Objects.isNull(price) ? 0 : price.getIntValue("minPrice");
		int maxPrice = Objects.isNull(price) ? 1000000000 : price.getIntValue("maxPrice");
		int minArea = Objects.isNull(area) ? 0 : area.getIntValue("minArea");
		int maxArea = Objects.isNull(area) ? 1000 : area.getIntValue("maxArea");
		keyword = keyword.equals("搜索房屋信息") ?"": keyword;
		System.out.println(keyword);
		Page<HouseInfo> pHI = houseDealService.findByPage(currentPage,minPrice,maxPrice,minArea,maxArea,Objects.isNull(layout) ? 0 : layout,keyword);
		return Objects.isNull(pHI) ? new Page<HouseInfo>() : pHI;
	}
	@RequestMapping("house-totalRent-ajax.do")
	@ResponseBody
	public Page<HouseInfo> housetotalRentAjax(@RequestParam("condition") String condition, @RequestParam(value = "currentPage", defaultValue = "1", required = false) int currentPage) {
		JSONObject o = JSON.parseObject(condition);
		JSONObject price = o.getJSONObject("price");
		Integer layout = o.getInteger("layout");
		String keyword = o.getString("keyword");
		int minPrice = Objects.isNull(price) ? 0 : price.getIntValue("minPrice");
		int maxPrice = Objects.isNull(price) ? 100000 : price.getIntValue("maxPrice");
		keyword = keyword.equals("搜索房屋信息") ?"": keyword;
		System.out.println(keyword);
		Page<HouseInfo> pHI = houseDealService.findByPage1(currentPage,minPrice,maxPrice,Objects.isNull(layout) ? 0 : layout,keyword);
		return Objects.isNull(pHI) ? new Page<HouseInfo>() : pHI;
	}
	@RequestMapping("house-divRent-ajax.do")
	@ResponseBody
	public Page<HouseInfo> housedivRentAjax(@RequestParam("condition") String condition, @RequestParam(value = "currentPage", defaultValue = "1", required = false) int currentPage) {
		JSONObject o = JSON.parseObject(condition);
		JSONObject price = o.getJSONObject("price");
		Integer layout = o.getInteger("layout");
		String keyword = o.getString("keyword");
		int minPrice = Objects.isNull(price) ? 0 : price.getIntValue("minPrice");
		int maxPrice = Objects.isNull(price) ? 100000 : price.getIntValue("maxPrice");
		keyword = keyword.equals("搜索房屋信息") ?"": keyword;
		System.out.println(keyword);
		Page<HouseInfo> pHI = houseDealService.findByPage2(currentPage,minPrice,maxPrice,Objects.isNull(layout) ? 0 : layout,keyword);
		return Objects.isNull(pHI) ? new Page<HouseInfo>() : pHI;
	}
	
	@RequestMapping("renterinfo.do")
	@ResponseBody
	public void renterInfo(@RequestParam("houseId") int houseId,HttpSession session)
	{
		List<Renter> renterList = houseDealService.getRenterById(houseId);
		session.setAttribute("renterList", renterList);
	}
}
